home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / logdaemon-2 / lib / sysv_utmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-30  |  2.3 KB  |  97 lines

  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3. #include <utmpx.h>
  4. #include <string.h>
  5. #include <syslog.h>
  6.  
  7. /* sysv_utmp_init - allocate utmp entry before login, update wtmp */
  8.  
  9. sysv_utmp_init(line, user, tag)
  10. char   *line;
  11. char   *user;
  12. char   *tag;
  13. {
  14.     struct utmpx utx;
  15.     int     num;
  16.  
  17.     memset((char *) &utx, 0, sizeof(utx));
  18.  
  19.     /* Derive utmp ID from pty slave number and application-specific tag. */
  20.  
  21.     if (sscanf(line, "%*[^0-9]%d", &num) != 1 || num > 255) {
  22.     syslog(LOG_ERR, "unparseable pty name: %s", line);
  23.     _exit(1);
  24.     }
  25.     sprintf(utx.ut_id, "%.2s%02x", tag, num);
  26.     strncpy(utx.ut_user, user, sizeof(utx.ut_user));
  27.     strncpy(utx.ut_line, line, sizeof(utx.ut_line));
  28.     utx.ut_pid = getpid();
  29.     utx.ut_type = LOGIN_PROCESS;
  30.     gettimeofday(&(utx.ut_tv));
  31.     pututxline(&utx);
  32.     updwtmpx(WTMPX_FILE, &utx);
  33.     endutxent();
  34. }
  35.  
  36. /* sysv_utmp_login - update utmp and wtmp after login */
  37.  
  38. sysv_utmp_login(line, user, host)
  39. char   *line;
  40. char   *user;
  41. char   *host;
  42. {
  43.     struct utmpx utx;
  44.     struct utmpx *ut;
  45.     pid_t   mypid = getpid();
  46.     int     ret = (-1);
  47.  
  48.     /*
  49.      * SYSV4 login cannot not use getutxline() here, because telnetd/rlogind
  50.      * create entries with line == /dev/pts/XXX, and other processes might do
  51.      * the same.
  52.      */
  53.  
  54.     while ((ut = getutxent())) {
  55.     if (ut->ut_pid == mypid
  56.      && (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)) {
  57.         strncpy(ut->ut_line, line, sizeof(ut->ut_line));
  58.         strncpy(ut->ut_user, user, sizeof(ut->ut_user));
  59.         strncpy(ut->ut_host, host, sizeof(ut->ut_host));
  60.         ut->ut_syslen = strlen(host) + 1;
  61.         if (ut->ut_syslen > sizeof(ut->ut_host))
  62.         ut->ut_syslen = sizeof(ut->ut_host);
  63.         ut->ut_type = USER_PROCESS;
  64.         gettimeofday(&(ut->ut_tv));
  65.         pututxline(ut);
  66.         updwtmpx(WTMPX_FILE, ut);
  67.         ret = 0;
  68.         break;
  69.     }
  70.     }
  71.     endutxent();
  72.     return (ret);
  73. }
  74.  
  75. /* sysv_utmp_logout - update utmp and wtmp after logout */
  76.  
  77. sysv_utmp_logout(line)
  78. char   *line;
  79. {
  80.     struct utmpx utx;
  81.     struct utmpx *ut;
  82.  
  83.     strncpy(utx.ut_line, line, sizeof(utx.ut_line));
  84.  
  85.     if ((ut = getutxline(&utx)) == 0) {
  86.     syslog(LOG_ERR, "%s: utmp entry not found", line);
  87.     } else {
  88.     ut->ut_type = DEAD_PROCESS;
  89.     ut->ut_exit.e_termination = 0;
  90.     ut->ut_exit.e_exit = 0;
  91.     gettimeofday(&(ut->ut_tv));
  92.     pututxline(ut);
  93.     updwtmpx(WTMPX_FILE, ut);
  94.     }
  95.     endutxent();
  96. }
  97.